{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16939df9",
   "metadata": {},
   "source": [
    "--------\n",
    "* 2022-11-01\n",
    "* week11\n",
    "* 记录人：拆拆"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21699b1f",
   "metadata": {},
   "source": [
    "## 本周学习目标\n",
    "> 1. 字典的方法 \n",
    "> 2. 实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ff9fa69c",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Alex\",\n",
    "    1800004:\"小明\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ef1b2579",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Alex', 1800004: '小明'} <class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(user_list,type(user_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4f00bd3f",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43muser_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[1;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "user_list[0] #证明字典是无序的， 没办法用index 索引取值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4b2533d",
   "metadata": {},
   "source": [
    "## 字典的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d73b044",
   "metadata": {},
   "source": [
    "## 1.1 数据结构的常见方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "392c763c",
   "metadata": {},
   "source": [
    "* 用 len() 查看长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9796c901",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a1551c",
   "metadata": {},
   "source": [
    "* in 查询成员是否存在：查询的是key是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a8a97778",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1800001 in user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a2d45aa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list[1800001]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "673ef9fa",
   "metadata": {},
   "source": [
    "## 1.2 字典的一些方法\n",
    "\n",
    "> 1.增删改查"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a58840e",
   "metadata": {},
   "source": [
    "* 字典的增加\n",
    "\n",
    "> 1. 先查询要增加的key是否存在（若存在，则不能直接增加，否则会被替代。）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "562eb75c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Alex', 1800004: '小明'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "57757275",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "增加成功~~\n"
     ]
    }
   ],
   "source": [
    "# 可以避免替换其他的key\n",
    "if 1800005 in user_list:\n",
    "    print(\"该用户已存在，请更换\")\n",
    "else:\n",
    "    user_list[1800005] = \"李华\"\n",
    "    print(\"增加成功~~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "aed27e29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d169f6cc",
   "metadata": {},
   "source": [
    "* 字典的修改\n",
    "> 1. 先查询要修改的key是否存在\n",
    "> 2. 如果不存在该key，可能需要考虑是否新增用户"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "db890812",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信息更新成功\n"
     ]
    }
   ],
   "source": [
    "if 1800001 in user_list:\n",
    "    user_list[1800001] = \"Jan\"\n",
    "    print(\"信息更新成功\")\n",
    "else:\n",
    "    print(\"该key找不到\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ba32e274",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jan',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Alex',\n",
       " 1800004: '小明',\n",
       " 1800005: '李华'}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "362e91e9",
   "metadata": {},
   "source": [
    "* 字典的查询 : get()   可以查询到该key的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4c4d0805",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jan'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.get(1800001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d1841b40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(user_list.get(1800009)) # 查询不到"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8894b2a6",
   "metadata": {},
   "source": [
    "# 1.2 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "cbe32282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jan'), (1800002, 'Marry'), (1800003, 'Alex'), (1800004, '小明'), (1800005, '李华')])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b35ec9b",
   "metadata": {},
   "source": [
    "* 字典的循环遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "61d7eece",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾 : 关于序列类型的数据 循环\n",
    "strings = \"hello world~\"\n",
    "lists = [18,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bb71ef9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n",
      "~\n"
     ]
    }
   ],
   "source": [
    "for i in strings:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b84511b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "19\n",
      "18\n",
      "20\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for i in lists:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "e82a2578",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001\n",
      "1800002\n",
      "1800003\n",
      "1800004\n",
      "1800005\n"
     ]
    }
   ],
   "source": [
    "for i in user_list:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "fb1f9de0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jan'), (1800002, 'Marry'), (1800003, 'Alex'), (1800004, '小明'), (1800005, '李华')])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "238c4715",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001 Jan\n",
      "1800002 Marry\n",
      "1800003 Alex\n",
      "1800004 小明\n",
      "1800005 李华\n"
     ]
    }
   ],
   "source": [
    "# 字典的循环\n",
    "for k,v in user_list.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2d02fbaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([1800001, 1800002, 1800003, 1800004, 1800005])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.keys() ：取出全部的key\n",
    "user_list.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "cb37e154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Jan', 'Marry', 'Alex', '小明', '李华'])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6efcb6f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800005, '李华')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# popitem()    随机删除1个\n",
    "user_list.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "88df32c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jan'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.pop(1800001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "99c5c9ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800002: 'Marry', 1800003: 'Alex', 1800004: '小明'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e279ac3",
   "metadata": {},
   "source": [
    "## 字典的高级用法\n",
    "\n",
    "* formkeys\n",
    "* setdefaut"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6e710e69",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 复位，  重启，  重置\n",
    "help(user_list.fromkeys)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e855bb8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Marry': 100, 'Alex': 100, '小明': 100}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置系统人物的属性，所有人族 hp（血量）10\n",
    "user_list.values()\n",
    "re_game = {}\n",
    "re_game.fromkeys(user_list.values(),100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d4538e8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "help(user_list.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "9a2765f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "books = {'runoob': '菜鸟教程','google':'Google 搜索'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "bca654ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'菜鸟教程'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.setdefault('runoob','菜鸟阅读')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "babac444",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'阿里阅读'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.setdefault('alibaba','阿里阅读')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "8f969843",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'runoob': '菜鸟教程', 'google': 'Google 搜索', 'alibaba': '阿里阅读'}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee004209",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
